﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PickGold.Util.WordSpliter
{
	public class Result : Keyword, IComparable<Result>
	{
		/// <summary>
		/// Current word type
		/// </summary>
		public ResultType Type;

		/// <summary>
		/// Original word type
		/// </summary>
		public ResultType Original;

		/// <summary>
		/// Word position
		/// </summary>
		public int Position;

		/// <summary>
		/// Rank for this word
		/// 单词权重
		/// </summary>
		public int Rank;

		public Result()
		{
		}

		public Result(string word, int position, KeywordFlags flags, double frequency, int rank, ResultType wordTye, ResultType originalWordType)
			: base(word, flags, frequency)
		{
			Position = position;
			Type = wordTye;
			Original = originalWordType;
			Rank = rank;
		}

		public Result(string word, KeywordFlags flags, double frequency) : base(word, flags, frequency) { }

		public Result(Keyword word)
		{
			this.Word = word.Word;
			this.Flags = word.Flags;
			this.Frequency = word.Frequency;
		}

		public Result(SearchMatch pl, string oringinal, MatchParameter parameters)
		{
			this.Word = oringinal.Substring(pl.Position, pl.Length);
			this.Flags = pl.Word.Flags;
			this.Frequency = pl.Word.Frequency;
			this.Type = ResultType.SimplifiedChinese;
			this.Position = pl.Position;
			if (parameters == null)
				return;

			switch (pl.Level)
			{
				case 0:
					this.Rank = parameters.BestRank;
					break;
				case 1:
					this.Rank = parameters.SecRank;
					break;
				case 2:
					this.Rank = parameters.ThirdRank;
					break;
				case 3:
					this.Rank = parameters.SingleRank;
					break;
				default:
					this.Rank = parameters.BestRank;
					break;
			}
		}

		public int GetEndPositon()
		{
			return this.Position + this.Word.Length;
		}

		public int CompareTo(Result other)
		{
			if (other == null)
				return -1;

			if (this.Position != other.Position)
				return this.Position.CompareTo(other.Position);

			if (other.Word == null)
				return -1;

			return this.Word.Length.CompareTo(other.Word.Length);
		}
	}

	public enum ResultType
	{
		None = 0,
		English = 1,
		SimplifiedChinese = 2,
		TraditionalChinese = 3,
		Numeric = 4,
		Symbol = 5,
		Space = 6,
		Synonym = 7, //同义词
	}
}
